<%@ include file="Common.jsp" %><%!
//
//   Filename: LinkNew.jsp
//   Generated with CodeCharge  v.1.2.0
//   JSP.ccp build 05/21/01
//

static final String sFileName = "AddLink.jsp";
              
%><%

boolean bDebug = false;

String sAction = getParam( request, "FormAction");
String sForm = getParam( request, "FormName");
String sLinkErr = "";
String sTreeErr = "";

java.sql.Connection conn = null;
java.sql.Statement stat = null;
String sErr = loadDriver();
conn = cn();
stat = conn.createStatement();
if ( ! sErr.equals("") ) {
 try {
   out.println(sErr);
 }
 catch (Exception e) {}
}
if ( sForm.equals("Link") ) {
  sLinkErr = LinkAction(request, response, session, out, sAction, sForm, conn, stat);
  if ( "sendRedirect".equals(sLinkErr)) return;
}
if ( sForm.equals("Tree") ) {
  sTreeErr = TreeAction(request, response, session, out, sAction, sForm, conn, stat);
  if ( "sendRedirect".equals(sTreeErr)) return;
}

%>            
<html>
<head>
<title>Links</title>
<meta name="GENERATOR" content="YesSoftware CodeCharge v.1.2.0 / JSP.ccp build 05/21/01"/>
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="expires" content="0"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body >
<jsp:include page="Header.jsp" flush="true"/>
<div class="container">
 <table class="table table-striped table-bordered table-condensed">
  <tr>
   
   <td valign="top">
<% Tree_Show(request, response, session, out, sTreeErr, sForm, sAction, conn, stat); %>
    
   </td>
  </tr>
 </table>
 <table class="table table-striped table-bordered table-condensed">
  <tr>
   <td valign="top">
<% Link_Show(request, response, session, out, sLinkErr, sForm, sAction, conn, stat); %>
    
   </td>
  </tr>
 </table>

</div>
 
</body>
</html>
<%%>
<%
if ( stat != null ) stat.close();
if ( conn != null ) conn.close();
%>
<%!


  String LinkAction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sAction, String sForm, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
  
    String sLinkErr ="";
    try {

      if (sAction.equals("")) return "";

      String sSQL="";
      String transitParams = "";
      String primaryKeyParams = "";
      String sQueryString = "";
      String sPage = "";
      String sParams = "";
      String sActionFileName = "LinkDetails.jsp";
      String sWhere = " ";
      boolean bErr = false;
      long iCount = 0;

  
      String pPKlink_id = "";
      if (sAction.equalsIgnoreCase("cancel") ) {
        try {
          if ( stat != null ) stat.close();
          if ( conn != null ) conn.close();
        }
        catch ( java.sql.SQLException ignore ) {}
        response.sendRedirect (sActionFileName);
        return "sendRedirect";
      }

      final int iinsertAction = 1;
      final int iupdateAction = 2;
      final int ideleteAction = 3;
      int iAction = 0;

      if ( sAction.equalsIgnoreCase("insert") ) { iAction = iinsertAction; }
      if ( sAction.equalsIgnoreCase("update") ) { iAction = iupdateAction; }
      if ( sAction.equalsIgnoreCase("delete") ) { iAction = ideleteAction; }

      // Create WHERE statement


      String fldcategory_id="";
      String fldname="";
      String fldlink_url="";
      String flddescription="";
      String fldadd_date="";
      String fldlink_id="";

      // Load all form fields into variables
    
      fldcategory_id = getParam( request, "Rqd_category_id");
      fldname = getParam(request, "name");
      fldlink_url = getParam(request, "link_url");
      flddescription = getParam(request, "description");
      fldadd_date = getParam(request, "add_date");
      // Validate fields
      if ( iAction == iinsertAction || iAction == iupdateAction ) {
        if ( isEmpty(fldname) ) {
          sLinkErr = sLinkErr + "The value in field Name* is required.<br>";
        }
        if ( isEmpty(fldlink_url) ) {
          sLinkErr = sLinkErr + "The value in field URL* is required.<br>";
        }
        if (sLinkErr.length() > 0 ) {
          return (sLinkErr);
        }
      }


      sSQL = "";
      // Create SQL statement

      switch (iAction) {
  
        case iinsertAction :
          
            sSQL = "insert into links (" + 
                "category_id," +
                "name," +
                "url," +
                "description," +
                "add_date)" +

                " values (" + 
                toSQL(fldcategory_id, adNumber) + "," +
                toSQL(fldname, adText) + "," +
                toSQL(fldlink_url, adText) + "," +
                toSQL(flddescription, adText) + "," +
                toSQL(fldadd_date, adDate) + ")";
          break;
  
      }

      if ( sLinkErr.length() > 0 ) return sLinkErr;
      try {
        // Execute SQL statement
        stat.executeUpdate(sSQL);
      }
      catch(java.sql.SQLException e) {
        sLinkErr = e.toString(); return (sLinkErr);
      }
  
      try {
        if ( stat != null ) stat.close();
        if ( conn != null ) conn.close();
      }
      catch ( java.sql.SQLException ignore ) {}
      response.sendRedirect (sActionFileName);

      return "sendRedirect";
    }
    catch (Exception e) {out.println(e.toString()); }
    return (sLinkErr);
  }

  


  void Link_Show(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sLinkErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
    try {

      String sSQL="";
      String sQueryString = "";
      String sPage = "";
      String sWhere = "";
      String transitParams = "";
      String transitParamsHidden = "";
      String requiredParams = "";
      String primaryKeyParams ="";
      java.util.Hashtable rsHash = new java.util.Hashtable();
      
      String plink_id = "";

      String fldcategory_id="";
      String fldlink_id="";
      String fldname="";
      String fldlink_url="";
      String flddescription="";
      String fldadd_date="";


      boolean bPK = true;

      if ( "".equals(sLinkErr)) {
        // Load primary key and form parameters
        fldcategory_id = getParam( request, "category_id");
        requiredParams += "<input type=\"hidden\" name=\"Rqd_category_id\" value=\""+getParam( request, "category_id")+"\">";
        plink_id = getParam( request, "link_id");
      }
      else {
        // Load primary key, form parameters and form fields
        fldlink_id = getParam( request, "link_id");
        fldname = getParam( request, "name");
        fldlink_url = getParam( request, "link_url");
        flddescription = getParam( request, "description");
        fldadd_date = getParam( request, "add_date");
        requiredParams += "<input type=\"hidden\" name=\"Rqd_category_id\" value=\""+getParam( request, "Rqd_category_id")+"\">";
        plink_id = getParam( request, "PK_link_id");
      }

      
      if ( isEmpty(plink_id)) { bPK = false; }
      
      sWhere += "link_id=" + toSQL(plink_id, adNumber);
      primaryKeyParams += "<input type=\"hidden\" name=\"PK_link_id\" value=\""+plink_id+"\"/>";

      sSQL = "select * from links where " + sWhere;


      out.println("    <table "+styleFormTABLE+">");
      out.println("     <tr>\n      <td "+styleFormHeaderTD+" colspan=\"2\"><font "+styleFormHeaderFONT+">New Link<br><font >(New links are inactive until approved)</font></font></td>\n     </tr>");
      if ( ! sLinkErr.equals("")) {
        out.println("     <tr>\n      <td "+styleDataTD+" colspan=\"2\"><font "+styleDataFONT+">"+sLinkErr+"</font></td>\n     </tr>");
      }
      sLinkErr="";
      out.println("     <form method=\"get\" action=\""+sFileName+"\" name=\"Link\">");

      java.sql.ResultSet rs = null;

      if ( bPK &&  ! (sAction.equals("insert") && "Link".equals(sForm))) {

        // Open recordset
        rs = openrs( stat, sSQL);
        rs.next();
        String[] aFields = getFieldsName( rs );
        getRecordToHash( rs, rsHash, aFields );
        rs.close();
        fldlink_id = (String) rsHash.get("link_id");
        fldcategory_id = (String) rsHash.get("category_id");
        fldadd_date = (String) rsHash.get("add_date");
        if ( "".equals(sLinkErr)) {
          // Load data from recordset when form displayed first time
          fldname = (String) rsHash.get("name");
          fldlink_url = (String) rsHash.get("url");
          flddescription = (String) rsHash.get("description");
        }

        if (sAction.equals("") || ! "Link".equals(sForm)) {
      
          fldlink_id = (String) rsHash.get("link_id");
          fldcategory_id = (String) rsHash.get("category_id");
          fldname = (String) rsHash.get("name");
          fldlink_url = (String) rsHash.get("url");
          flddescription = (String) rsHash.get("description");
          fldadd_date = (String) rsHash.get("add_date");
        }
        else {
          fldlink_id = (String) rsHash.get("link_id");
          fldcategory_id = (String) rsHash.get("category_id");
          fldadd_date = (String) rsHash.get("add_date");
        }
        
      }
      else {
        if ( "".equals(sLinkErr)) {
          fldcategory_id = toHTML(getParam(request,"category_id"));
        }
fldadd_date = new java.text.SimpleDateFormat("yyyy-MM-dd").format(new java.util.Date());
      }
      
      // Set lookup fields
          fldcategory_id = dLookUp( stat, "categories", "category_desc", "category_id=" + toSQL(fldcategory_id, adNumber));


      // Show form field
      
      out.print("     <tr>\n      <td "+styleFieldCaptionTD+"><font "+styleFieldCaptionFONT+">Category</font></td><td "+styleDataTD+">"); out.print("<font "+styleDataFONT+">"+toHTML(fldcategory_id)+"&nbsp;</font>");
      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td "+styleFieldCaptionTD+"><font "+styleFieldCaptionFONT+">Name*</font></td><td "+styleDataTD+">"); out.print("<input type=\"text\"  name=\"name\" maxlength=\"50\" value=\""+toHTML(fldname)+"\" size=\"50\">");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td "+styleFieldCaptionTD+"><font "+styleFieldCaptionFONT+">URL*</font></td><td "+styleDataTD+">"); out.print("<input type=\"text\"  name=\"link_url\" maxlength=\"50\" value=\""+toHTML(fldlink_url)+"\" size=\"50\">");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td "+styleFieldCaptionTD+"><font "+styleFieldCaptionFONT+">Description</font></td><td "+styleDataTD+">"); out.print("<textarea name=\"description\" cols=\"50\" rows=\"5\">"+toHTML(flddescription)+"</textarea>");

      out.println("</td>\n     </tr>");
      
      out.print("     <tr>\n      <td colspan=\"2\" align=\"right\">");
      

      if ( bPK && ! (sAction.equals("insert") && "Link".equals(sForm))) {
        out.print("<input class=\"btn btn-danger\" type=\"submit\" value=\"Cancel\" onclick=\"document.Link.FormAction.value = 'cancel';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Link\"><input type=\"hidden\" value=\"\" name=\"FormAction\">");
      }
      
      else {
        out.print("<input class=\"btn btn-success\" type=\"submit\" value=\"Insert\" onclick=\"document.Link.FormAction.value = 'insert';\">");out.print("<input class=\"btn btn-danger\" type=\"submit\" value=\"Cancel\" onclick=\"document.Link.FormAction.value = 'cancel';\">");
        out.print("<input type=\"hidden\" name=\"FormName\" value=\"Link\"><input type=\"hidden\" value=\"insert\" name=\"FormAction\">");
      }out.print("<input type=\"hidden\" name=\"link_id\" value=\""+toHTML(fldlink_id)+"\">");out.print("<input type=\"hidden\" name=\"add_date\" value=\""+toHTML(fldadd_date)+"\">");
      out.print(transitParamsHidden+requiredParams+primaryKeyParams);
      out.println("</td>\n     </tr>\n     </form>\n    </table>");
      



    }
    catch (Exception e) { out.println(e.toString()); }
  } 

  String sActionFileName = "AddLink.jsp";

      
  String TreeAction(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sAction, String sForm, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
    try {
      
      String sCatId = getParam( request, "category_id");
      String sOp = "=";
      if ( isEmpty(sCatId) || ! isNumber(sCatId) ) {
        sCatId = "null";
        sOp = " is ";
      }
      long lNRec = dCountRec( stat, "categories", "parent_category_id"+ sOp + sCatId);
      if ( lNRec == 0 ) {
        try {
          if ( stat != null ) stat.close();
          if ( conn != null ) conn.close();
        }
        catch ( java.sql.SQLException ignore ) {}
        response.sendRedirect("AddLink.jsp?category_id=" + sCatId);
        return "sendRedirect";
      
      }
    }
    catch (Exception e) { out.println(e.toString()); };
    return ("");
  }
      
  void Tree_Show(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.http.HttpSession session, javax.servlet.jsp.JspWriter out, String sTreeErr, String sForm, String sAction, java.sql.Connection conn, java.sql.Statement stat) throws java.io.IOException {
    try {
  
      String transitParams = "";
      String sQueryString = "";
      String sPage = "";
      String sSQL = "select category_id, category_desc, parent_category_id from categories";
      String sCatID = getParam( request, "category_id");
      String sWhere = "";
      java.sql.ResultSet rs = null;
      String sCatPath = "";
      String sCatList = "";
      String sParCatID = "";
      String sCatName  = "";
      String sPath = "";
      String stCatID    = "";
      String stParCatID = "";
      String stCatName  = "";
  
      out.println("    <table "+styleFormTABLE+">");
      out.println("     <tr>\n      <td "+styleFormHeaderTD+"><font "+styleFormHeaderFONT+">Select Category</font></td>\n     </tr>");
      out.println("     <tr>\n      <td "+styleColumnTD+"><nobr><font "+styleColumnFONT+"><a href=\""+sActionFileName+"?FormName=Tree\"><font "+styleColumnFONT+">Top</font></a>");
  
      
      if (sCatID == null || sCatID.compareTo("") == 0 || ! isNumber(sCatID)) {
        // Root category
        sWhere = " where parent_category_id is Null";
      }
      else {
        
        // Subcategory
        sWhere = " where category_id=" + sCatID;
        rs = openrs ( stat, sSQL + sWhere);
        if ( rs.next()) {
          java.util.Hashtable rsHash = new java.util.Hashtable();
          String[] aFields = getFieldsName( rs );
          getRecordToHash( rs, rsHash, aFields );
  
          sCatPath = "";
          sCatList = "";
          sParCatID = toHTML((String) rsHash.get("parent_category_id"));
          sCatName  = toHTML((String) rsHash.get("category_desc"));
        }
        rs.close();
        sPath = " > "+sCatName+"</font></nobr></td>\n     </tr>";
  
        
        // Build Path
        stParCatID = sParCatID;
        while ( stParCatID.compareTo("") != 0 ) {
          rs = openrs ( stat, sSQL + " where category_id=" + stParCatID);
          java.util.Hashtable rsHash = new java.util.Hashtable();
          String[] aFields = getFieldsName( rs );
          if ( rs.next() ) {
            getRecordToHash( rs, rsHash, aFields );
            stCatID    = toHTML((String) rsHash.get("category_id"));
            stParCatID = toHTML((String) rsHash.get("parent_category_id"));
            stCatName  = toHTML((String) rsHash.get("category_desc"));
            sPath = "> <a href=\""+sActionFileName+"?FormName=Tree&category_id="+stCatID+"\"><font "+styleColumnFONT+">"+stCatName+"</font></a>" + sPath;
          }
          rs.close();
        }
        out.println(sPath);
        sWhere = " where parent_category_id=" + sCatID;
      }
  
      
      // Categories list
      rs = openrs ( stat, sSQL + sWhere);
      if (rs != null) {
        java.util.Hashtable rsHash = new java.util.Hashtable();
        String[] aFields = getFieldsName( rs );
        while ( rs.next() ) {
          // Print subcategories
          getRecordToHash( rs, rsHash, aFields );
          stCatID    = toHTML((String) rsHash.get("category_id"));
          stParCatID = toHTML((String) rsHash.get("parent_category_id"));
          stCatName  = toHTML((String) rsHash.get("category_desc"));
          
          out.println("    <tr>\n     <td "+styleDataTD+"><a href=\""+sActionFileName+"?FormName=Tree&category_id="+stCatID+"\"><font "+styleDataFONT+">"+stCatName+"</font></a></td>\n     </tr>");
        }
      }
      rs.close();
    
      out.println("    </table>");
    }
    catch (Exception e) { out.println(e.toString()); };
  }   

%>